﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using OSharp.Utility;
using OSharp.Utility.Data;
using OSharp.Web.Mvc.Security;
using OSharp.Web.Mvc.UI;
using Serenity.Utility.Mvc.UI;

namespace Serenity.Libra.Web.Controllers.Systems
{
    [Description("管理-菜单")]
    public class MenuController : AdminBaseController
    {
        public ISystemContract SystemContract { get; set; }

        [Description("管理-菜单-列表")]
        public override ActionResult Index()
        {
            return base.Index();
        }

        #region Ajax功能
        #region 获取数据

        //id: 组织机构编号
        [AjaxOnly]
        [Description("管理-角色-列表数据")]
        public ActionResult TreeData(int? id)
        {
            int total = 0;
            MiniGridRequest request = new MiniGridRequest(Request);
            var list = GetQueryData<Menu, Guid>(SystemContract.Menus, out total, request);
            return Json(list.Select(m => new
            {
                m.Id,
                m.Name,
                m.Code,
                m.Icon,
                Parent_Id = m.Parent == null ? null : m.Parent.Id.ToString()
            }), JsonRequestBehavior.AllowGet);
        }

        #endregion
        #region 功能方法

        [HttpPost]
        [AjaxOnly]
        [Description("管理-菜单-新增")]
        public ActionResult Add(MenuInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.AddMenus(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-菜单-编辑")]
        public ActionResult Edit(MenuInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.EditMenus(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-菜单-删除")]
        public ActionResult Delete(Guid[] ids)
        {
            ids.CheckNotNull("ids");
            OperationResult result = SystemContract.DeleteMenus(ids);
            return Json(result.ToAjaxResult());
        }
        [AjaxOnly]
        [Description("管理-品牌-获取信息")]
        public ActionResult Menu(Guid id)
        {
            var data = SystemContract.Menus.Include(o => o.Parent).First(o => o.Id == id);
            var outData = new MenuOutputDto();
            return Json(data.MapTo(outData), JsonRequestBehavior.AllowGet);
        }

        #endregion
        #endregion
    }
}